package com.sskj.hangqing.presenter;

import com.lzy.okgo.request.GetRequest;
import com.sskj.common.base.HttpData;
import com.sskj.hangqing.ui.fragment.CoinListFragment;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.http.CallBackOption;
import com.sskj.lib.util.CoinImgUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;


public class CoinFragmentPresenter extends BasePresenter<CoinListFragment> {

    /**
     * 行情数据
     *
     * @return
     */
    public void getProduct(UserData userData, String qu, boolean isUpDown, boolean isNew, boolean isSelf) {
        GetRequest<HttpData<List<CoinBean>>> product;
        if (isUpDown) {
            product = httpService.getProduct(qu, null);
        } else if (isNew) {
            product = httpService.getProduct(qu, "2");
        } else if (isSelf) {
            if (userData == null) {
                mView.updateUI(new ArrayList<>());
                return;
            }
            product = httpService.getProduct(qu, "1");
        } else {
            product = httpService.getProduct(qu);
        }
        product.tag(this).execute(new CallBackOption<HttpData<List<CoinBean>>>() {
        }.unLoadBind(mView).execute(httpData -> {
            if (httpData.getData() != null) {
                Disposable disposable = Flowable.fromIterable(httpData.getData())
                        .filter(coinBean -> coinBean.getCode() != null)
                        .map(coinBean -> {
                            CoinImgUtil.img.put(coinBean.getCode(), coinBean.getImg());
                            return coinBean;
                        })
                        .toList()
                        .subscribe(coinBeans -> {
                            mView.updateUI(coinBeans);
                        }, e -> {
                        });
            }
        }));
    }
}
